-- global_weaken
-- created by panyl
-- 通用的削弱目标类技能(对所有怪物起效)，与冈布奥自身属性无关，就是按所配数值；受加成，BOSS轮数减半

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = -1;
        local bossRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
            bossRound = round + lastRound + 1;
        elseif para[5] > 0 then
            effectRound = round + para[5] + 1;
            bossRound = math.modf(round + para[5] / 2 + 1);
        end

        local addonRound = 0;
        local prop = PropM.combine(source, "magic_last", skillId);
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "assist");
        addonRound = PropM.apply(prop, addonRound);
        prop = PropM.combine(source, "magic_last", "all");
        addonRound = PropM.apply(prop, addonRound);

        effectRound = effectRound + addonRound;
        bossRound   = math.modf(bossRound + addonRound / 2);

        local targets = {};
        local grids = DungeonM.getCurrentDungeon();
        for _, grid in pairs(grids) do
            if  grid:isMonster() and
                grid:isOpened() and
                not grid.monster:isDead() then

                -- 添加光效
                if SkillM.getSequence(grid.monster) == nil then
                    SkillM.getSequenceList()[grid.monster] = CastSequence.create(source, grid.monster, skillId);
                end

                SkillM.getSequence(grid.monster):hit(source, grid.monster, skillId);

                table.insert(targets, grid.monster);
            end
        end

        -- 数值
        local value = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终数值
        value = math.modf(value * (addon_rate + 1000)/1000);

        local condition = {
            ["id"] = para[2],
            ["value"] = value,
            ["end_round"] = effectRound,
        };
        local conditionBoss = {
            ["id"] = para[2],
            ["value"] = math.modf(value / 2),
            ["end_round"] = bossRound,
        };
        if "1" ~= para[3] then
            condition["key"] = para[3];
        end

        if para[6] == 1 then
            -- 需要计算禁锢抗性的
            for _, target in pairs(targets) do
                -- 需要判断是否存在禁锢抗性
                local prop = PropM.combine(target, "durance_resist", 1);
                if PropM.apply(prop, 1) > 0 then
                    -- 只提示BOSS的
                    if target:isBoss() and not target:isDead() then
                        if prop[4] == -1 then
                            doAlert(getLocStr("no_durance2"));
                        else
                            doAlert(string.format(getLocStr("no_durance"), (prop[4] - round)));
                        end
                    end
                else
                    -- 附加状态
                    if target:isBoss() then
                        CombatStatusM.applyStatus(target, para[1], conditionBoss);
                    else
                        CombatStatusM.applyStatus(target, para[1], condition);
                    end
                end
            end
        else
            -- 不计算禁锢抗性的
            for _, target in pairs(targets) do
                if target:isBoss() then
                    CombatStatusM.applyStatus(target, para[1], conditionBoss);
                else
                    CombatStatusM.applyStatus(target, para[1], condition);
                end
            end
        end
        -- 返回作用目标
        return targets;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), arr[3], tonumber(arr[4]), tonumber(arr[5]), tonumber(arr[6]) or -1, };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 威力
        local base = para[4];
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        if PropM.query(para[2], "type") == PROP_TYPE_RATIO then
            -- 显示为百分比的
            base = math.modf(base / 10);
            force = math.modf(force / 10);
        end

        -- 效果/计算时魔力+3
        local addon = force - base;
        if addon == 0 then
            desc = string.gsub(desc, "{global_weaken}", force);
        else
            desc = string.gsub(desc, "{global_weaken}", base .. "(+" .. addon .. ")");
        end

        -- 回合数的显示
        local baseRound = para[5];
        local bossRound = math.modf(para[5] / 2);
        local round = para[5];
        -- 增加持续时间的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
            desc = string.gsub(desc, "{boss_round}", bossRound);
        else
            local addonRoundBoss = math.modf(addonRound / 2);
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
            desc = string.gsub(desc, "{boss_round}", bossRound .. "(+" .. addonRoundBoss .. ")");
        end

        return desc;
    end,
};
